var catbox = document.getElementsByClassName('catbox')[0];
var checkAll = document.getElementsByClassName("check-all")[0];
var checkOneList = document.getElementsByClassName("check-one");


catbox.onclick = function (e) {
  var e = e || window.event;
  var target = e.target || e.srcElement;

  if (target.classList.contains('check-all')) {
    checkAllHandler(target);
    getTotal();

  } else if (target.classList.contains('check-one')) {
    checkOneHandler();
    getTotal();

  } else if (target.className == 'add') {

    addHandler(target);
    getTotal();

  } else if (target.className == 'reduce') {
    reduceHandler(target);
    getTotal();

  } else if (target.className == 'delete') {

    deleteOneHandler(target);
    getTotal();
  } else if (target.id == 'deleteAll') {
    deletAllHandler();
    isAllChecked();
    getTotal();
  }
}

function checkAllHandler (target) {
  console.log(target);
  var status = target.checked;
  for (i = 0; i < checkOneList.length; i++) {
    var checkOne = checkOneList[i];
    checkOne.checked = status;

  }
}

function checkOneHandler () {
  var flag = true; // 假设全部被选中   => 找到一个未选中的即可推翻假设
  for (var j = 0; j < checkOneList.length; j++) {
    var item = checkOneList[j];
    if (!item.checked) {  //item.checked == false
      flag = false;
      break;
    }
  }
  checkAll.checked = checkOneList.length > 0 ? flag : false;
}

function addHandler (target) {
  var countInp = prev(target);  // target => 被点击的元素(加号)
  console.log(countInp);
  var num = countInp.value;  // 取值
  num++;  //自增
  countInp.value = num; //赋值(更新到页面)

  // b.小计更新  =  单价*数量
  // 数量  =>  num
  // 单价  =>  先找单价的元素
  var countTd = parent(target);
  var priceTd = prev(countTd);
  var subtotalTd = next(countTd);

  var price = priceTd.innerText * 1;

  subtotalTd.innerText = (price * num).toFixed(2);

  // c. 减号按钮的 “-”  出现 
  var reduce = prev(countInp);
  console.log(reduce);
  reduce.textContent = "-";

}

function reduceHandler (target) {
  var countInp = next(target);
  var num = countInp.value;  // 取值

  // c. 减之前  判断  如果数量为1  不能减  
  if (num == 1) {
    return false; //  函数遇到return就停止了 后续的代码不在执行
  }
  num--;  //自减
  // d. 减之后  判断  如果数量为1  减号按钮的 “-” 消失  (2切换到1)
  if (num == 1) {
    target.textContent = "";
  }

  countInp.value = num; //赋值(更新到页面)

  // b.小计更新  =  单价*数量
  // 数量  =>  num
  // 单价  =>  先找单价的元素
  var countTd = parent(target);
  var priceTd = prev(countTd);
  var subtotalTd = next(countTd);

  var price = priceTd.innerText * 1;

  subtotalTd.innerText = (price * num).toFixed(2);
}

function deleteOneHandler (target) {
  console.log(target);
  if (confirm("是否删除该商品?")) {
    var tr = parent(parent(target));
    tr.remove();
    // 删除之后  判断 剩余的商品全部被选
    isAllChecked();
  }
}

function deletAllHandler () {
  for (var i = 0; i < checkOneList.length; i++) {
    var checkOne = checkOneList[i];
    if (checkOne.checked) {
      var tr = parent(parent(checkOne));
      console.log(tr);
      tr.remove();
      i--;
    }
  }
}


function parent (ele) {
  return ele.parentElement || ele.parentNode;
}
function prev (ele) {
  return ele.previousElementSibling || ele.previousSibling;
}
function next (ele) {
  return ele.nextElementSibling || ele.nextSibling;
}

function isAllChecked () {
  var flag = true; // 假设全部被选中   => 找到一个未选中的即可推翻假设
  for (var j = 0; j < checkOneList.length; j++) {
    var item = checkOneList[j];
    if (!item.checked) {  //item.checked == false
      flag = false;
      break;
    }
  }
  checkAll.checked = checkOneList.length > 0 ? flag : false;
}

// 找到所有单选中 所有为选中状态的商品 累加数量和小计
function getTotal () {
  var sum = 0;
  var allPrice = 0;
  for (var j = 0; j < checkOneList.length; j++) {
    var checkOne = checkOneList[j];
    if (checkOne.checked) {
      var tr = parent(parent(checkOne));
      // console.log(tr);
      var countInp = tr.getElementsByClassName("count-input")[0];
      var subtotalTd = tr.getElementsByClassName("subtotal")[0];
      var num = countInp.value * 1;
      var subtotal = subtotalTd.textContent * 1;
      // console.log(num,subtotal);

      sum += num;
      allPrice += subtotal;
    }
  }

  console.log(sum, allPrice);
  selectedTotal.textContent = sum;
  priceTotal.textContent = allPrice.toFixed(2);
}